﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace PCCardN
{
    /// <summary>
    /// 读写系统配置xml文件。
    /// </summary>
    public class ConfigurationFile
    {
        /// <summary>
        /// 写入key值
        /// </summary>
        public  bool SetKeyValue(string key, string value)
        {
            //增加的内容写在appSettings段下 <add key="RegCode" value="0"/>
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            try
            {
                if (config.AppSettings.Settings[key] == null)
                {
                    config.AppSettings.Settings.Add(key, value);
                }
                else
                {
                    config.AppSettings.Settings[key].Value = value;
                }
                config.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection("appSettings");
            }
            catch (Exception)
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 读取指定key的值
        /// </summary>
        public  string GetKeyValue(string key)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] == null)
                return "";
            else
                return config.AppSettings.Settings[key].Value;
        }
    }
}
